#include <bits/stdc++.h>
using namespace std;

char buf[(int) 5e7], *fs = buf;

inline int gn() {
    int k = 0;
    register char c = *fs++;
    for (; !isdigit(c); c = *fs++);
    for (; isdigit(c); c = *fs++) k = k * 10 + c - '0';
    return k;
}

int x, y, n, q, a[8003];
set<int> vis;

int main() {
    freopen("a.in", "r", stdin);
    freopen("a.out", "w", stdout);
    fread(buf, 1, (int) 5e7, stdin);   
    n = gn(), q = gn();
    for (int i = 1; i <= n; i++)
        a[i] = gn(), vis.insert(a[i]);
    while (q--) {
        x = gn(), y = gn();
        bool ix = vis.count(x), iy = vis.count(y);
        if (!ix and !iy) {
            cout << ((n * (n + 1)) >> 1) << '\n';
        }
        else if ((ix and iy) or x == y) {
            int ans = 0, xt, yt;
            for (int i = 1; i <= n; i++) {
                xt = 0, yt = 0;
                for (int j = i; j <= n; j++) {
                    if (a[j] == x) xt++;
                    if (a[j] == y) yt++;
                    ans += (xt == yt);
                }
            }
            cout << ans << '\n';
        }
        else {
            cout << 0 << '\n';
        }
    }
}
